//
//  TaskManager.m
//  XNet
//
//  Created by easystudio on 6/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TaskManager.h"
#import "UserTask.h"

static TaskManager* _oShared;
static NSMutableDictionary* _Tasks;
@implementation TaskManager


+ (TaskManager*) Share
{
    if (!_oShared) {
        _oShared = [[TaskManager alloc ] init];
    }
    
    return _oShared;
}

-(id) init
{
    self = [super init];
    _Tasks = [[NSMutableDictionary alloc] init];
    _oShared = self;
    return self;
}

-(UserTask*) GetTaskByType:(NSString*)type Delegate:(NSObject *)delegate
{
     UserTask* tmp = [_Tasks objectForKey:type];
    if (tmp == nil ) {
        UserTask* tsk = [UserTask alloc];
        [tsk InitWithType:type Delegate:delegate];
        //UserTask* tsk = [[UserTask alloc] init];
        [_Tasks setObject:tsk forKey:type];
        [tsk release];
        tmp = tsk;
    }
    [tmp setDelegate:(NSObject<UserTaskDelegate> *)delegate];
    return tmp;
}

-(void) dealloc
{
    [super dealloc];
    
    for (UserTask* del in _Tasks) {
        [del cancel];
        [del release];
    }

    [_Tasks release];
}
@end
